package com.cqgc.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.cqgc.properties.SMSProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class AliyunSmsUtil {

    @Autowired
    private SMSProperties smsProperties;


    /**
     * 发送短信
     *
     * @param phoneNumbers 手机号
     */
    public boolean sendSms(String phoneNumbers, String code) {
        try {
            DefaultProfile profile = DefaultProfile.getProfile(smsProperties.getRegionId(), smsProperties.getAccessKeyId(), smsProperties.getAccessKeySecret());
            //getProfile(地域编码,AccessKeyId,AccessKeySecret)
            IAcsClient client = new DefaultAcsClient(profile);
//            System.out.println(smsProperties.getRegionId());
            SendSmsRequest request = new SendSmsRequest();
            request.setSysRegionId(smsProperties.getRegionId());//设置地域编码
            request.setPhoneNumbers(phoneNumbers);
            request.setSignName(smsProperties.getSignName());
            request.setTemplateCode(smsProperties.getTemplateCode());
            request.setTemplateParam("{\"code\":\"" + code + "\"}");

            SendSmsResponse response = client.getAcsResponse(request);
//            System.out.println("短信发送成功");
        } catch (ClientException e) {
            e.printStackTrace();
            return false;

        }
        return true;

    }


}



